package com.newworld.basketballfire.main.business;

import com.newworld.basketballfire.main.api.Const;
import com.newworld.basketballfire.main.api.DuesType;
import com.newworld.basketballfire.main.api.ErrorCode;
import com.newworld.basketballfire.main.bean.UserListParam;
import com.newworld.basketballfire.main.dao.UserDao;
import com.newworld.basketballfire.main.utils.CommonUtils;
import com.newworld.basketballfire.main.utils.InputChecker;
import com.newworld.basketballfire.service.exception.ServiceException;
import com.newworld.basketballfire.service.model.Dues;
import com.newworld.basketballfire.service.model.ListResult;
import com.newworld.basketballfire.service.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;

/**
 * Created by George on 2019/3/3.
 * UserBusiness
 */
@Component
public class UserBusiness {
    private static Logger logger = LoggerFactory.getLogger(UserBusiness.class);

    @Autowired
    private UserDao userDao;

    public User createUser(User user) throws ServiceException {
        checkUser(user);

        user.setId(CommonUtils.getUUID());
        long currentTime = CommonUtils.getCurrentTime();
        user.setCreateTime(currentTime);
        user.setLastUpdateTime(currentTime);
        user.setVersion(1);

        Dues dues;
        if (user.getDues().compareTo(BigDecimal.ZERO) != 0) {
            dues = getDuesByUser(user);
        } else {
            dues = null;
        }
        userDao.createUser(user, dues);

        return user;
    }

    private Dues getDuesByUser(User user) {
        Dues dues = new Dues();
        dues.setId(CommonUtils.getUUID());
        dues.setType(getDuesTypeByValue(user.getDues()));
        dues.setUserId(user.getId());
        dues.setActivityId(Const.DEFAULT_ACTIVITY_ID);
        dues.setDues(user.getDues());
        dues.setDate(user.getJoinDate());
        dues.setInfo(Const.DEFAULT_USER_DUES_INFO);
        dues.setLastUpdateTime(CommonUtils.getCurrentTime());
        dues.setVersion(Const.DEFAULT_VERSION);
        return dues;
    }

    private int getDuesTypeByValue(BigDecimal dues) {
        if (dues.compareTo(BigDecimal.ZERO) >= 0) {
            return DuesType.RECHARGE.getValue();
        }
        return DuesType.CONSUME.getValue();
    }

    private void checkUser(User user) throws ServiceException {
        if (StringUtils.isEmpty(user.getName())) {
            logger.error("name is null or empty.");
            throw new ServiceException(HttpStatus.BAD_REQUEST, ErrorCode.INVALID_PARAMS, "name is null or empty.");
        }
    }

    public User updateUser(User user) throws ServiceException {
        User oldUser = userDao.getUser(user.getId());
        if (oldUser == null || !user.getId().equals(oldUser.getId())) {
            logger.error("user not exist. user id={}.", user.getId());
            throw new ServiceException(HttpStatus.BAD_REQUEST, ErrorCode.INVALID_PARAMS, "user not exist.");
        }

        user.setVersion(oldUser.getVersion());
        userDao.updateUser(user);
        user.setVersion(oldUser.getVersion() + 1);
        return user;
    }

    public User getUser(String id) throws ServiceException {
        return userDao.getUser(id);
    }

    public ListResult<User> getUserList(UserListParam userListParam) throws ServiceException {
        InputChecker.checkPageSizeAndNo(userListParam.getPageNo(), userListParam.getPageSize());

        int curIndex = (userListParam.getPageNo() - 1) * userListParam.getPageSize();
        userListParam.setCurIndex(curIndex);

        ListResult<User> result = new ListResult<>();
        result.setTotalCnt(userDao.getUserListCount(userListParam));
        result.setPageNo(userListParam.getPageNo());
        result.setPageSize(userListParam.getPageSize());
        result.setData(userDao.getUserList(userListParam));

        return result;
    }

}
